package com.ftg.learn.service.impl;

import com.ftg.learn.entity.EmployeeEntity;
import com.ftg.learn.mapper.EmployeeMapper;
import com.ftg.learn.service.EmployeeService;
import com.ftg.learn.util.PageEx;
import com.ftg.learn.util.PageUtils;
import com.ftg.learn.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 布衣
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {


    @Resource
    EmployeeMapper employeeMapper;

    @Override
    public R<PageEx<EmployeeEntity>> showAll(String ename, Integer page, Integer size) {
        int begin = (page - 1) * size;
        PageEx<EmployeeEntity> pageEx = new PageEx<>(page, size);
        pageEx.setCount(employeeMapper.getCount(ename));
        pageEx.setRows(employeeMapper.showall(ename, begin, size));
        return R.ok(pageEx);
    }

    @Override
    public List<EmployeeEntity> showAllByName(String ename) {
        return employeeMapper.showAllByName(ename);
    }

    @Override
    public R<Integer> addEmployee(EmployeeEntity employeeEntity) {
        return R.ok(employeeMapper.addEmployee(employeeEntity));
    }

    @Override
    public R<Integer> deleteEmployee(int eid) {
        return R.ok(employeeMapper.deleteEmployee(eid));
    }

    @Override
    public R<Integer> updateEmployee(EmployeeEntity employeeEntity) {
        return R.ok(employeeMapper.updateEmployee(employeeEntity));
    }

    @Override
    public R<Integer> deleteByIds(List<Integer> eids) {
        return R.ok(employeeMapper.deleteByIds(eids));
    }

    @Override
    public R<PageEx<EmployeeEntity>> pageHelperAll(String ename, int page, int size) {
        PageHelper.startPage(page, size); // 启动分页
        PageInfo<EmployeeEntity> pageInfo = new PageInfo<>(employeeMapper.pageHelperAll(ename)); // 自动生成分页信息
        PageEx<EmployeeEntity> pageEx = PageUtils.convertToPageEx(pageInfo, page, size);
        return R.ok(pageEx);
    }

    @Override
    public R<PageInfo<EmployeeEntity>> pageTest(String ename, int page, int size) {
        PageHelper.startPage(page, size);
        PageInfo<EmployeeEntity> pageInfo = new PageInfo<>(employeeMapper.pageTest(ename));
        return R.ok(pageInfo);
    }

}
